package com.intellagric.controller.agritech.controller;

import com.alibaba.dubbo.common.json.ParseException;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.common.utils.JsonUtils;
import com.intellagric.pojo.AgtTag;
import com.intellagric.service.agritech.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author: cwq
 * @Date: 2019/2/21 12:15
 * @Description:
 */
@RequestMapping("/api/agt_tag")
@Controller
public class TagController {

    @Autowired
    private TagService tagService;

    @RequestMapping("/add")
    @ResponseBody
    public ResponseMessage add(AgtTag tag) {
        return tagService.addTag(tag);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResponseMessage delete(String id) {
        return tagService.deleteTag(id);
    }

    @RequestMapping("/edit")
    @ResponseBody
    public ResponseMessage edit(AgtTag tag) {
        return tagService.editTag(tag);
    }

    @RequestMapping("/{id}")
    @ResponseBody
    public AgtTag get(@PathVariable String id) {
        return tagService.getTagById(id);
    }

    @RequestMapping("/tree")
    @ResponseBody
    public LayuiDataGridResult getTree() {
        return tagService.getTagTree();
    }

    @RequestMapping("/tree/select")
    @ResponseBody
    public String getTreeSelect() throws ParseException {
        String data = JsonUtils.objectToJson(tagService.getTagTree().getData());
        return data.replaceAll("tagValue", "name");
    }

    @RequestMapping("/list")
    @ResponseBody
    public LayuiDataGridResult getList() {
        return tagService.getTagList();
    }
}
